package com.liangzhong.proxy.compete
{
	import com.liangzhong.commandconst.Compete.SMDCompete;
	import com.liangzhong.commandconst.commandType.MainCommandType;
	import com.liangzhong.constants.HttpRequestConst;
	import com.liangzhong.errortip.ErrorTips;
	import com.liangzhong.events.AppEventDispatcher;
	import com.liangzhong.events.CompeteEvent;
	import com.liangzhong.global.CMD_Head;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.compete.vo.CompeteMatchResultVo;
	import com.liangzhong.model.friend.vo.FriendInfoVo;
	import com.liangzhong.proxy.BaseProxy;
	import com.liangzhong.proxy.ProxyUtils;
	import com.liangzhong.proxyconstant.compete.CompeteProxyConstant;
	import com.socket.clientsocket.IClientSocket;
	
	import flash.net.URLVariables;
	import flash.utils.ByteArray;
	
	import lz.controls.Alert;
	
	public class CompeteProxy extends BaseProxy
	{
		public static const PROXYNAME:String = "CompeteProxy";
		public function CompeteProxy(proxyName:String=PROXYNAME, data:Object=null)
		{
			super(proxyName, data);
		}
		override public function onSocketRead(mainCmdID:uint, subCmdID:uint, buffer:ByteArray, dataSize:int, clientSocket:IClientSocket):Boolean
		{
			switch(mainCmdID)
			{
				//				case CMD_Public.MAIN://公用协议
				//					if(SMDCompete.BUYTREASURY == subCmdID)
				//					{
				//						getbuy(buffer);
				//						return true;
				//					}
				//					return false;
				case MainCommandType.MATCH:
					return onDeal(subCmdID,buffer,dataSize,clientSocket);
				case MainCommandType.PUBLICCOMMANDTYPE:
					return onPubliccommand(subCmdID,buffer,dataSize,clientSocket);
				case MainCommandType.MATCH_RESULE:
					onMatchResult(subCmdID,buffer,dataSize,clientSocket);
					return true;
				case MainCommandType.TASK:
					
					return onTaskHal(subCmdID,buffer,dataSize,clientSocket);
			}
			return false;
		}
		private function onDeal(subCmdID:uint, buffer:ByteArray, dataSize:int, clientSocket:IClientSocket):Boolean
		{
			var results:int;
			switch(subCmdID)
			{
				case SMDCompete.SELECTCHANGLLENATION:
					getselectchallengenation(buffer);
					return true;
				case SMDCompete.COMPETEMATCH:
					getCompteMatch(buffer);
					return true;
			}
			return false;
		}
		/**
		 *40处理40协议 
		 * @param subCmdID
		 * @param buffer
		 * @param dataSize
		 * @param clientSocket
		 * @return 
		 * 
		 */
		private function onPubliccommand(subCmdID:uint, buffer:ByteArray, dataSize:int, clientSocket:IClientSocket):Boolean
		{
			
			switch(subCmdID)
			{
				case SMDCompete.DONATE:
					getdonate(buffer);
					return true;
				case SMDCompete.BUYTREASURY:
					getbuy(buffer);
					return true;
//				case SMDCompete.BUYTREASURY:
//					getbuy(buffer);
//					return true;
			}
			return false;
		}
		/**
		 *处理比赛结果  
		 * 
		 */
		private function onMatchResult(subCmdID:uint, buffer:ByteArray, dataSize:int, clientSocket:IClientSocket):Boolean
		{
			var nreturn:int ;
			switch(subCmdID)
			{
				case SMDCompete.MATCHRESULE:
					if(AppData.getInstance().competeDataManager.competeInfoVo)
					{
						nreturn = buffer.readInt();
						if(nreturn == 0)
						{
							var vo:CompeteMatchResultVo = new CompeteMatchResultVo()
							vo.analysis(buffer);
							this.sendNotification(CompeteProxyConstant.GET_MATCH_RESULT,{vo:vo});
						}
					}
					return true;
				case SMDCompete.RIVAL_CHANGE://获得挑战点
//					dddd
					nreturn= buffer.readInt();
					var value:int = buffer.readInt();
					AppData.getInstance().competeDataManager.competeInfoVo.rival = value;
					AppData.getInstance().competeDataManager.rerankCurrent();
					return true;
			}
			return false;
		}
		/**
		 * 任务相关 
		 * @param subCmdID
		 * @param buffer
		 * @param dataSize
		 * @param clientSocket
		 * @return 
		 * 
		 */		
		private function onTaskHal(subCmdID:uint, buffer:ByteArray, dataSize:int, clientSocket:IClientSocket):Boolean
		{
			switch(subCmdID)
			{
				case SMDCompete.ISSUSTASK:
					getIssusTask(buffer);
					return true;
					break;
			}
			return false;
		}
		/**
		 *请求国王和三大将的信息 
		 * 
		 */
		public function kingAndCompetesenior($obj:Object):void
		{
			var variables:URLVariables = ProxyUtils.instance.getUrlVariables();
			variables.method = HttpRequestConst.KANDSENIOR;
			variables.country = $obj.id;
			ProxyUtils.instance.webClient(getkingAndCompeteSenior,variables,false);
		}
		/**
		 *返回国王和三大将信息 
		 * 
		 */
		private function getkingAndCompeteSenior($obj:Object):void
		{
			var obj:Object = ProxyUtils.instance.analysisHttpData($obj,CompeteProxy);
			if(obj.retcode == 0)
			{
				var arr:Array = AppData.getInstance().competeDataManager.analysisnationRank(obj.topfour);
				this.sendNotification(CompeteProxyConstant.GETKINGANDSENIOR,{arr:arr,nationRank:obj.nationrank});
			}else
			{
				Alert.showErrorTip(ErrorTips.instance.getErrorTip(obj.retcode));
			}
		}
		/**
		 *发布的任务ID 
		 */
		private var _issTaskId:int;
		/**
		 *请求发布任务 
		 * @param $obj
		 * 
		 */
		public function requestIssusTask($obj:Object):void
		{
			

			
			var sendBuf :ByteArray = 
				CMD_Head.getHeadByteArrayBuff(CMD_Head.Size + 4,MainCommandType.TASK,SMDCompete.ISSUSTASK);
			var id:int = $obj.id;
			sendBuf.writeInt(id);
			_issTaskId = id;
			sendBuf.position = 0;
			clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
			
			
//			AppData.getInstance().competeDataManager.issueNationTask(_issTaskId);
//			AppEventDispatcher.getInstance().dispatchEvent(new CompeteEvent(CompeteEvent.TASKISSSUCCESS));
		}
		/**
		 *返回发布任务 
		 * 
		 */
		private function getIssusTask(buffer:ByteArray):void
		{
			var nReturn:int = buffer.readInt();
			if(nReturn ==0)
			{
				AppData.getInstance().competeDataManager.issueNationTask(_issTaskId);
				AppData.getInstance().competeDataManager.competeInfoVo.homecoin = buffer.readInt();
				Alert.showErrorTip("发布成功");
				AppEventDispatcher.getInstance().dispatchEvent(new CompeteEvent(CompeteEvent.TASKISSSUCCESS));
			}
			else
			{
				Alert.showErrorTip(ErrorTips.instance.getCErrorTip(nReturn));
			}
			
//			AppData.getInstance().competeDataManager.issueNationTask(_issTaskId);
//			Alert.showErrorTip("发布成功");
//			AppEventDispatcher.getInstance().dispatchEvent(new CompeteEvent(CompeteEvent.TASKISSSUCCESS));
		}
		/**
		 *请求国王选择挑战国家 
		 * @param $obj
		 * 
		 */
		public function requestselectchallengenation($obj:Object):void
		{
			var sendBuf :ByteArray = 
				CMD_Head.getHeadByteArrayBuff(CMD_Head.Size + 2,MainCommandType.MATCH,SMDCompete.SELECTCHANGLLENATION);
			var id:int = $obj.id;
			sendBuf.writeShort(id);
			sendBuf.position = 0;
			clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
		}
		/**
		 *返回国王挑战国家 
		 * 
		 */
		private function getselectchallengenation(buffer:ByteArray):void
		{
			var nReturn:int = buffer.readInt();
			if(nReturn ==0)
			{
				requestCompeteInfo();
				
				Alert.showErrorTip("选择成功");
			}
			else
			{
				Alert.showErrorTip(ErrorTips.instance.getCErrorTip(nReturn));
			}
		}
		/**
		 *请求比赛 
		 * @param $obj
		 * 
		 */
		public function requestCompeteMatch($obj:Object):void
		{
			var sendBuf :ByteArray = 
				CMD_Head.getHeadByteArrayBuff(CMD_Head.Size + 5,MainCommandType.MATCH,SMDCompete.COMPETEMATCH);
			sendBuf.writeInt($obj.id);
			var id:int = $obj.formation;
			sendBuf.writeByte(id);
			sendBuf.position = 0;
			clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
		}
		/**
		 *返回比赛 
		 * @param buffer
		 * 
		 */
		private function getCompteMatch(buffer:ByteArray):void
		{
			var nReturn:int = buffer.readInt();
			if(nReturn ==0)
			{
				this.sendNotification(CompeteProxyConstant.GET_CHALLENGE);
				//				Alert.showErrorTip("选择成功");
			}
			else
			{
				Alert.showErrorTip(ErrorTips.instance.getCErrorTip(nReturn));
				this.sendNotification(CompeteProxyConstant.GET_CHALLENGE);
			}
		}
		/**
		 *请求购买宝库物品 
		 * @param $obj
		 * 
		 */
		public function requestbuy($obj:Object):void
		{
//			requestdonate($obj);
//			return;
			var sendBuf :ByteArray = 
				CMD_Head.getHeadByteArrayBuff(CMD_Head.Size + 6,MainCommandType.PUBLICCOMMANDTYPE,SMDCompete.BUYTREASURY);
			sendBuf.writeByte($obj.type);
			sendBuf.writeInt($obj.pid);
			sendBuf.writeByte($obj.ptype);
			
			sendBuf.position = 0;
			clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
		}
		/**
		 *返回购买宝库物品 
		 * @param buffer
		 * 
		 */
		private function getbuy(buffer:ByteArray):void
		{
			var nReturn:int = buffer.readInt();
			if(nReturn ==0)
			{
				Alert.showErrorTip("购买成功");
			}
			else
			{
				Alert.showErrorTip(ErrorTips.instance.getCErrorTip(nReturn));
			}
		}
		
		
		/**
		 *请求国家挑战信息 
		 * 
		 */
		public function requestCompeteInfo():void
		{
			var variables:URLVariables = ProxyUtils.instance.getUrlVariables();
			variables.method = HttpRequestConst.COMPETEINFO;
			ProxyUtils.instance.webClient(getCompetInfo,variables,false);
		}
		/**
		 * 返回国家挑战信息 
		 * @param $data
		 * 
		 */		
		private function getCompetInfo($data:Object):void
		{
			var obj:Object = ProxyUtils.instance.analysisHttpData($data,CompeteProxy);
			if(obj.retcode == 0)
			{
				AppData.getInstance().competeDataManager.analysiscompeteInfo(obj);
				AppData.getInstance().competeDataManager.competeInfoVo.isupdateopponentinfo = false;
				this.sendNotification(CompeteProxyConstant.GETCOMPETEINFO,{nationRank:obj.nationrank});
			}else
			{
				Alert.showErrorTip(ErrorTips.instance.getErrorTip(obj.retcode));
			}
		}
		/**
		 *请求捐赠 
		 * @param $obj
		 * 
		 */
		public function requestdonate($obj:Object):void
		{
			var sendBuf :ByteArray = 
				CMD_Head.getHeadByteArrayBuff(CMD_Head.Size+ 1 ,MainCommandType.PUBLICCOMMANDTYPE,SMDCompete.DONATE);
			sendBuf.writeByte($obj.type);
			sendBuf.position = 0;
			clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
		}
		/**
		 *返回捐赠  
		 * @param buffer
		 * 
		 */
		private function getdonate(buffer:ByteArray):void
		{
			var nReturn:int = buffer.readInt();
			if(nReturn ==0)
			{
				var m:int = buffer.readInt();
				AppData.getInstance().competeDataManager.competeInfoVo.homecoin = m;
				this.sendNotification(CompeteProxyConstant.GETDONATE);
				Alert.showErrorTip("捐赠成功");
			}
			else
			{
				Alert.showErrorTip(ErrorTips.instance.getCErrorTip(nReturn));
			}
		}
		/**
		 *请求其他俱乐部信息s 
		 * @param $obj
		 * 
		 */
		public function requestOtherInfo($obj:Object):void
		{
			var variables:URLVariables = ProxyUtils.instance.getUrlVariables();
			variables.method = HttpRequestConst.OTHER_INFO;
			variables.gcid = $obj.id;
			ProxyUtils.instance.webClient(getOtherInfo,variables,false);
		}
		/**
		 *返回其他俱乐部信息 
		 * @param $data
		 * 
		 */
		private function getOtherInfo($data:Object):void
		{
			var obj:Object = ProxyUtils.instance.analysisHttpData($data,CompeteProxy);
			if(obj.retcode == 0)
			{
				var vo:FriendInfoVo = new FriendInfoVo();
				vo.analsysBaseInfo(obj.clubinfo);
				vo.hightPlayer(obj.player);
				vo.friendid = obj.clubid;
				this.sendNotification(CompeteProxyConstant.GETOTHER_INFO,{vo:vo});
			}else
			{
				Alert.showErrorTip(ErrorTips.instance.getErrorTip(obj.retcode));
			}
		}
		/**
		 *获取更多的排名 
		 * @param $obj
		 * 
		 */
		public function requestMoreInlandRank($obj:Object):void
		{
			var variables:URLVariables = ProxyUtils.instance.getUrlVariables();
			variables.method = HttpRequestConst.MOREINLANDRANK;
			variables.type = $obj.type;
			ProxyUtils.instance.webClient(getMoreInlandRank,variables,false);
		}
		/**
		 *返回更多的排名  
		 * @param $data
		 * 
		 */
		private function getMoreInlandRank($data:Object):void
		{
			var obj:Object = ProxyUtils.instance.analysisHttpData($data,CompeteProxy);
			if(obj.retcode == 0)
			{
				AppData.getInstance().competeDataManager.analyscurrentList(obj.nationrank_current);
				AppData.getInstance().competeDataManager.competeInfoVo.rival = obj.selfrival;
				AppData.getInstance().competeDataManager.competeInfoVo.nationrank = obj.selfsort;
				if(obj.nationrank_previous != null)
				{
					AppData.getInstance().competeDataManager.analysipreviousList(obj.nationrank_previous);
				}
				AppData.getInstance().competeDataManager.moreinlandRankTime = AppData.getInstance().time;
				this.sendNotification(CompeteProxyConstant.GETMOREINLANDRANK);
			}else
			{
				Alert.showErrorTip(ErrorTips.instance.getErrorTip(obj.retcode));
			}
		}
		/**
		 *请求取国库资金 
		 * 
		 */
		public function requesttreasuryfunds():void
		{
			var variables:URLVariables = ProxyUtils.instance.getUrlVariables();
			variables.method = HttpRequestConst.COMPETETREASURY;
			ProxyUtils.instance.webClient(gettreasuryfunds,variables,false);
		}
		private function gettreasuryfunds($data:Object):void
		{
			var obj:Object = ProxyUtils.instance.analysisHttpData($data,CompeteProxy);
			if(obj.retcode == 0)
			{
				AppData.getInstance().competeDataManager.competeInfoVo.homecoin = obj.coin
				this.sendNotification(CompeteProxyConstant.GETDONATE);
			}else
			{
				Alert.showErrorTip(ErrorTips.instance.getErrorTip(obj.retcode));
			}
		}
	}
}